package pers.mingshan.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author hanjuntao
 * @date 2024/12/21 0021
 */
public class CopyOnWriteArrayListTest {

    public void arrayListTest() {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        Iterator<String> iterator = list.iterator();
        for (int i = 0; i < 1; i++) {
            threadPool.execute(() -> {
                for (int j = 0; j < 5; j++) {
                    list.add(j * 8 + "");
                }
            });
        }

        for (int i = 0; i < 10; i++) {
            threadPool.execute(() -> {
                while (iterator.hasNext()) {
                    System.out.println(iterator.next());
                }
            });
        }
    }
}
